// http://cppcms.sourceforge.net/wikipp/en/page/cppcms_1x_tut_url_mapping
// http://cppcms.sourceforge.net/wikipp/en/page/cppcms_1x_tut_hello_templates


#include <cppcms/application.h>  
#include <cppcms/applications_pool.h>  
#include <cppcms/service.h>  
#include <cppcms/http_response.h>  
#include <cppcms/url_dispatcher.h>  
#include <iostream>  

#include "content.hpp"


class hello : public cppcms::application 
{  
public:  
        
    hello(cppcms::service &srv) 
        : cppcms::application(srv)  
    {  
        dispatcher().assign("/number/(\\d+)", &hello::number, this, 1);  
        dispatcher().assign("/smile", &hello::smile, this);  
        dispatcher().assign("", &hello::welcome, this);  
    }          
        
        
    //virtual void main(std::string url);
    virtual void main(std::string /*url*/)  
    {  
        content::message c;
        c.text=">>>Hello<<<";  
        render("message",c);  
    }          
        
    
    void number(std::string num)  
    {  
        int no = atoi(num.c_str());  
        response().out() << "The number is " << no << "\n";  
    }  
    
    void smile()  
    {  
        response().out() << ":-)";  
    }  
    
    void welcome()  
    {  
        response().out() <<  
            "<h1> Wellcome To Page with links </h1>\n"  
            "<a href=\"/hello/number/1\">1</a><br>\n"  
            "<a href=\"/hello/number/15\">15</a><br>\n"  
            "<a href=\"/hello/smile\">:-)</a><br>\n";  
    }      
};  

//void hello::main(std::string url)  
//{  
//    response().out() <<  
//        "<html>\n"  
//        "<body>\n"  
//        "  <h1>Hello World</h1>\n"  
//        "</body>\n"  
//        "</html>\n";  
//}  

int main(int argc,char ** argv)  
{  
    try 
    {
        cppcms::service srv(argc, argv);  
        
        srv.applications_pool().mount( cppcms::applications_factory<hello>() ); 
        srv.run();  
    }  
    catch( std::exception const & e ) 
    {  
        std::cerr << e.what() << std::endl;  
    }          
        
}
